package com.qzq.weibo.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.qzq.weibo.R;
import com.qzq.weibo.activity.ReplyAndRetweetActivity;
import com.qzq.weibo.entity.Status;
import com.qzq.weibo.utils.Utils;
import com.qzq.weibo.view.CircleImageView;

public class StatusAdapter extends ArrayAdapter<Status> {

	private LayoutInflater mInflater;
	private int resourceId;
	private Utils utils;

	private static final String TAG = "StatusAdapter";

	public StatusAdapter(Context context, int resource, List<Status> objects) {
		super(context, resource, objects);

		this.resourceId = resource;
		this.mInflater = LayoutInflater.from(context);
		this.utils = Utils.getInstance(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		Status status = getItem(position);
		Status retweeted_status = status.getRetweeted_status();
		List<String> pic_urls = status.getPic_urls();

		View view = null;
		if (convertView == null) {
			view = mInflater.inflate(resourceId, null);
		} else {
			view = convertView;
		}

		/** 微博主体 */
		CircleImageView iv_avatar = (CircleImageView) view
				.findViewById(R.id.item_status_iv_user_avatar);
		TextView tv_screen_name = (TextView) view
				.findViewById(R.id.item_status_tv_user_screen_name);
		TextView tv_created_at = (TextView) view
				.findViewById(R.id.item_status_tv_created_at);
		TextView tv_source = (TextView) view
				.findViewById(R.id.item_status_tv_source);
		TextView tv_text = (TextView) view
				.findViewById(R.id.item_status_tv_text);

		/** 转发微博主体 */
		View divier = (View) view
				.findViewById(R.id.item_status_divier_retweeted_status);
		LinearLayout ll_retweeted_status = (LinearLayout) view
				.findViewById(R.id.item_status_ll_retweeted_status);
		TextView tv_retweeted_status_user_screen_name = (TextView) view
				.findViewById(R.id.item_status_tv_retweeted_status_user_screen_name);
		TextView tv_retweeted_status_text = (TextView) view
				.findViewById(R.id.item_status_tv_retweeted_status_text);

		/** 微博主体图片 */
		LinearLayout ll_pics = (LinearLayout) view
				.findViewById(R.id.item_status_ll_pics);
		LinearLayout ll_retweet_pics = (LinearLayout) view
				.findViewById(R.id.item_retweet_status_ll_pics);

		/** 微博底部控件 */
		LinearLayout ll_reply = (LinearLayout) view
				.findViewById(R.id.item_status_bar_reply);
		LinearLayout ll_retweet = (LinearLayout) view
				.findViewById(R.id.item_status_bar_retweet);
		ImageView iv_like = (ImageView) view
				.findViewById(R.id.item_status_iv_like);
		TextView tv_like_num = (TextView) view
				.findViewById(R.id.item_status_tv_like_num);
		TextView tv_reply_num = (TextView) view
				.findViewById(R.id.item_status_tv_reply_num);
		TextView tv_retweet_num = (TextView) view
				.findViewById(R.id.item_status_tv_retweet_num);

		/** 异步加载头像 */
		utils.loadBitmap(status.getUser().getAvatar_large(), iv_avatar, 40, 40);

		tv_screen_name.setText(status.getUser().getScreen_name());
		tv_created_at.setText(utils.setSimpleDateTime(status.getCreated_at()));
		tv_source.setText(status.getSource());
		tv_text.setText(status.getText());

		/** 异步加载微博图片 */
		if (pic_urls.size() > 0) {
			ll_pics.removeAllViews();
			for (int i = 0; i < pic_urls.size(); i++) {
				ll_pics.addView(insertPhoto(pic_urls.get(i), 300, 300));
			}
		} else {
			ll_pics.removeAllViews();
		}

		/** 是否为转发微博 */
		if (retweeted_status != null) {
			List<String> retweeted_pic_urls = retweeted_status.getPic_urls();
			divier.setVisibility(View.VISIBLE);
			ll_retweeted_status.setVisibility(View.VISIBLE);
			tv_retweeted_status_user_screen_name.setText("@"
					+ retweeted_status.getUser().getScreen_name());
			tv_retweeted_status_text.setText(retweeted_status.getText());

			/** 转发微博的图片 */
			if (retweeted_pic_urls.size() > 0) {
				ll_retweet_pics.removeAllViews();
				for (int i = 0; i < retweeted_pic_urls.size(); i++) {
					ll_retweet_pics.addView(insertPhoto(
							retweeted_pic_urls.get(i), 150, 150));
				}
			} else {
				ll_retweet_pics.removeAllViews();
			}

		} else {

			divier.setVisibility(View.GONE);
			ll_retweeted_status.setVisibility(View.GONE);
			tv_retweeted_status_user_screen_name.setText("");
			tv_retweeted_status_text.setText("");
		}

		/** 微博底部各个信息的数量显示 */
		tv_like_num.setText(String.valueOf(status.getAttitudes_count()));
		tv_reply_num.setText(String.valueOf(status.getComments_count()));
		tv_retweet_num.setText(String.valueOf(status.getReposts_count()));

		/** 评论微博 */
		ll_reply.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickReply(position);
			}
		});

		/** 转发微博 */
		ll_retweet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickRetweet(position);
			}
		});
		return view;
	}

	/** 评论微博 */
	private void onClickReply(int position) {
		Intent intent = null;
		intent = new Intent(getContext(), ReplyAndRetweetActivity.class);
		intent.putExtra("idstr", getItem(position).getIdstr());
		intent.putExtra("actionReponse", "reply_status");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		getContext().startActivity(intent);
	}

	/** 转发微博 */

	private void onClickRetweet(int position) {

		Intent intent = null;
		intent = new Intent(getContext(), ReplyAndRetweetActivity.class);
		intent.putExtra("idstr", getItem(position).getIdstr());
		intent.putExtra("actionReponse", "retweet");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		getContext().startActivity(intent);

	}

	/** 向微博中添加图片 */

	private View insertPhoto(String urlStr, int width, int height) {

		LinearLayout layout = new LinearLayout(getContext());
		layout.setLayoutParams(new LayoutParams(width, width));
		layout.setGravity(Gravity.CENTER);

		ImageView imageView = new ImageView(getContext());
		imageView.setLayoutParams(new LayoutParams(width - 10, width - 10));
		imageView.setScaleType(ScaleType.CENTER_CROP);
		utils.loadBitmap(urlStr, imageView, width, height);

		layout.addView(imageView);
		return layout;
	}

}
